﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Telerik.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZP.UI.PopWindows;

namespace Baosight.ZNH.ZP.UI
{
    public partial class ZP14 : EFViewCmes
    {
        public ZP14()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.Grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击所选择的列表发生改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (null != this.Grid.SelectedItem)
            {
                EIRow _row = (this.Grid.SelectedItem as UIBaseEntity).GetEIRow();
                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("roleId", _row["postLabel"].ToString());
                this.area1.LoadView<EFCustomViewContainerPanel>("ZP15", args, viewLoaded, null);//加载从表页面
            }
            else
            {
                this.area1.Content = null;
                this.area2.Content = null;
            }
        }

        /// <summary>
        /// 先加载资源组页面，再加载用户页面
        /// 原因是：若一起加载按钮会乱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void viewLoaded(object sender, RoutedEventArgs e)
        {
            EIRow _row = (this.Grid.SelectedItem as UIBaseEntity).GetEIRow();
            Dictionary<string, string> args1 = new Dictionary<string, string>();
            args1.Add("roleId", _row["postLabel"].ToString());
            this.area2.LoadView<EFCustomViewContainerPanel>("ZP16", args1);//加载从表页面
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "角色-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.Grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();

            if (_list.Count > 0)
            {
                EIBlock eIBlock = eiInfo.AddBlock("result");
                eIBlock.SetRow(0, BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]));
            }

            base.OnUpdateButtonClickPopup(eiInfo, "角色-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.Grid.Refresh();//刷新Grid区域
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo eiInfo, string _title, string _dialogType, string _args)
        {
            Role view = new Role(_dialogType);//构造一个新的修改页面
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            view.EIDataContext.Update(eiInfo);

            if (_dialogType.Equals("insert"))
            {
                view.orgLabel.Value = this.EIDataContext.GetAttribute("orgId");
            }

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterOwner;
            _popup.TopOffset = -100;
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = view;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }
    }
}
